#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QScreen>
#include <QDebug>

using namespace cv;
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //parameter init
    camera_f = ui->cameraFocuslineEdit->text().toDouble();
    camera_h = ui->cameraHeightlineEdit->text().toDouble();
    //mouse signal
    connect(ui->videoLabel,&VideoLabel::mouseMoveSignal,
            [=](int x,int y,bool isOutside)
    {
        if(isOutside)
            ui->statelabel->setText(QString("outside"));
        else
            ui->statelabel->setText(QString("(%1,%2)").arg(QString::number(x)).arg(QString::number(y)));
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::videoOption()
{
    if(videoDS == Closed)
    {
        capture.open(ui->camChooseSpinBox->text().toInt());
        if(capture.isOpened())
        {
            capture >> frame;

            connect(videoTimer, &QTimer::timeout,
                    [=]()
            {
                this->showVideo();
            });
            videoTimer->start(30);
            ui->camOpenButton->setText("关闭相机");
            ui->camDeviceStateLabel->setText("相机打开");
            videoDS = Opened;
        }
        else
        {
            ui->camDeviceStateLabel->setText("打开失败");
        }
    }
    else
    {
        capture.release();
        videoDS = Closed;
        ui->camOpenButton->setText("打开相机");
        ui->camDeviceStateLabel->setText("相机关闭");
        if(videoTimer->isActive())
        {
            videoTimer->stop();
        }
    }
}

void MainWindow::showVideo()
{
    capture >> frame;
    Mat frameShow = frame.clone();
    cvtColor(frameShow,frameShow,CV_BGR2RGB);
    this->ui->videoLabel->setPixmap(QPixmap::fromImage(QImage(frameShow.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888)));
}

void MainWindow::on_camOpenButton_clicked()
{
    videoOption();
}

double MainWindow::calBallDiameter(double d)
{
    double l = -12.567;
    return 2*d*(camera_h+l)/(2*camera_f+d);
}

void MainWindow::on_saveImagepushButton_clicked()
{
    if(videoDS == Opened)
    {
        QDir *temp =new QDir;
        if(!temp->exists(QCoreApplication::applicationDirPath()+"/pic"))
            temp->mkdir(QCoreApplication::applicationDirPath()+"/pic");
        QString path = QCoreApplication::applicationDirPath()+"/pic/s"+QString::number(pictureCount) + QString(".jpg");
        qDebug()<<path;
        QScreen *screen = QGuiApplication::primaryScreen();
        screen->grabWindow(ui->videoLabel->winId()).save(path);
        pictureCount++;
    }
}

void MainWindow::on_startCalculatepushButton_clicked()
{
    if(ui->algorithmSelectcomboBox->currentIndex()==0)
    {
        double d = ui->diameterInputlineEdit->text().toDouble();
        double ballDiameter = calBallDiameter(d);
        ui->outputtextBrowser->append(QString("测量直径为：")+QString::number(ballDiameter));
    }
}

void MainWindow::on_cameraFocuslineEdit_editingFinished()
{
    camera_f = ui->cameraFocuslineEdit->text().toDouble();
}

void MainWindow::on_cameraHeightlineEdit_editingFinished()
{
    camera_h = ui->cameraHeightlineEdit->text().toDouble();
}
